package com.lwz.laboratory.service;

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.lwz.laboratory.dao.RegisterMapper;
import com.lwz.laboratory.dao.RepairMapper;
import com.lwz.laboratory.entity.Repair;
import com.sun.deploy.net.URLEncoder;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepairService {
    @Autowired
    RepairMapper repairMapper;

    public Repair findone(Integer id) {
        return repairMapper.selectByPrimaryKey(id);
    }

    public void  addrepair(Repair repair ){
        repair.setAddtime(new Timestamp(System.currentTimeMillis()));
        repairMapper.insertSelective(repair);
    }

    public List<Repair> findall(String laboratory){
        return repairMapper.getRepList(laboratory);
    }
    public List<Repair> selectByperson(String applicant){
        return repairMapper.selectByperson(applicant);
    }

    public void exportPdf(HttpServletResponse response, Integer id){
        boolean flag=true;
        String path = "C:\\Users\\15122\\Desktop\\boot期末";
        //获取数据
        Repair repair = repairMapper.selectByPrimaryKey(id);


        // 指定解析器
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
        String filename="表.pdf";
        response.setContentType("application/pdf");


        try {
//            int i=1/0;
            response.setHeader("Content-Disposition", "attachment;fileName="
                    + URLEncoder.encode(filename, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        }
        OutputStream os = null;
        PdfStamper ps = null;
        PdfReader reader = null;
        try {
            os = response.getOutputStream();
            // 2 读入pdf表单(给予导出表单pdf的文件miing)
            reader = new PdfReader(path+ "/"+filename);
            // 3 根据表单生成一个新的pdf
            ps = new PdfStamper(reader, os);
            // 4 获取pdf表单
            AcroFields form = ps.getAcroFields();
            // 5给表单添加中文字体 这里采用系统字体。不设置的话，中文可能无法显示
//            BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",
//                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            //这边设置pdf字体，可以更改字体的格式
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            form.addSubstitutionFont(bf);


            Date a =repair.getAddtime();
            SimpleDateFormat b=new SimpleDateFormat("YYYY-MM-dd");
            String c =b.format(a);

            // 6查询数据(将数据赋给Map集合，key的值对应Pdf模板的值，value对应数据库中取出的)
            Map<String,Object> data = new HashMap<>();
            data.put("equipmentid",repair.getEquipmentid());
            data.put("equipmentname",repair.getEquipmentname());
            data.put("model",repair.getModel());
            data.put("content",repair.getContent());
            data.put("addtime",c);
            data.put("applicant",repair.getApplicant());
            data.put("laboratory",repair.getLaboratory());

            // 7遍历data 给pdf表单表格赋值
            for (String key : data.keySet()) {
                form.setField(key, MapUtils.getString(data, key));
            }
            ps.setFormFlattening(true);
            flag=true;
        } catch (Exception e) {
            flag=false;
            e.printStackTrace();

        } finally {
            try {
                ps.close();
                reader.close();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
